package com.alexander.cloundexplorer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alexander.cloundexplorer.R;
import com.alexander.cloundexplorer.bean.params.LoginParams;
import com.alexander.cloundexplorer.bean.params.RegisterParams;
import com.alexander.cloundexplorer.bean.params.User;
import com.alexander.cloundexplorer.util.Constants;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * @author Administrator
 */
public class LoginActivity extends BaseActivity
{

    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.login)
    Button login;
    @BindView(R.id.registerr)
    Button registerr;

    private RegisterParams registerParams = new RegisterParams();
    private LoginParams loginParams = new LoginParams();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.login, R.id.registerr})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.login:
                loginParams.setAccount(phone.getText().toString());
                loginParams.setPassword(password.getText().toString());
                System.out.println("IP地址：" + Constants.IP);
                OkHttpUtils
                        .post()
                        .url(Constants.IP.concat("/userController/login"))
                        .addParams("params", JSON.toJSONString(loginParams))
                        .build()
                        .execute(new StringCallback()
                        {
                            @Override
                            public void onError(Call call, Exception e, int id)
                            {
                                showTip(e.getMessage());
                            }

                            @Override
                            public void onResponse(String response, int id)
                            {
                                showTip(response);
                                if(response.contains("200"))
                                {
                                    JSONObject jsonObject = JSON.parseObject(response);
                                    String data = jsonObject.getString("data");
                                    User user = JSON.parseObject(data, User.class);
                                    user.setPassword(loginParams.getPassword());
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    intent.putExtra("user", user);
                                    LoginActivity.this.startActivity(intent);
                                    finish();
                                }
                            }
                        });
                break;
            case R.id.registerr:
                registerParams.setAccount(phone.getText().toString());
                registerParams.setPassword(password.getText().toString());
                OkHttpUtils
                        .post()
                        .url(Constants.IP.concat("/userController/register"))
                        .addParams("params", JSON.toJSONString(registerParams))
                        .build()
                        .execute(new StringCallback()
                        {
                            @Override
                            public void onError(Call call, Exception e, int id)
                            {
                                showTip(e.getMessage());
                            }

                            @Override
                            public void onResponse(String response, int id)
                            {
                                showTip(response);
                            }
                        });
                break;
            default:
                break;
        }
    }
}
